<template>
  <div class="login-container">
    <div class="title-container">
      <h3 class="title">智慧工地信息系统平台</h3>
    </div>
    <div class="content">
      <div class="left">
        <img src="../../assets/images/login-img.png" alt="">
      </div>
      <div class="right">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
          <div class="title-img">
            <img src="../../assets/images/revisePwd.png" alt="">
          </div>

          <el-form-item prop="oldPassword">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="oldPassword"
              v-model="loginForm.oldPassword"
              :type="passwordType"
              placeholder="初始密码"
              name="oldPassword"
              tabindex="1"
              auto-complete="off"
            />
          </el-form-item>
          <el-form-item prop="newPassword">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="newPassword"
              v-model="loginForm.newPassword"
              :type="passwordType"
              placeholder="新密码"
              name="newPassword"
              tabindex="2"
              auto-complete="off"
            />
          </el-form-item>
          <el-form-item prop="surepassword">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="surepassword"
              v-model="loginForm.surepassword"
              :type="passwordType"
              placeholder="确认密码"
              name="surepassword"
              tabindex="3"
              auto-complete="off"
              @keyup.enter.native="sureLogin"
            />
          </el-form-item>
          <div style="display: flex;">
            <el-button type="primary" style="width:50%;margin-bottom:30px;" @click.native.prevent="backLogin">取消</el-button>
            <el-button :loading="loading" type="primary" style="width:50%;margin-bottom:30px;" @click.native.prevent="sureLogin">确认</el-button>
          </div>

        </el-form>
      </div>
    </div>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { updatePassword } from '@/api/user'

export default {
  name: 'RevisePwd',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length <= 20 && value.length >= 8) {
        callback()
      } else {
        callback(new Error('密码由8-20个大小写字母、数字、特殊字符组成'))
      }
    }
    const validateNewPassword = (rule, value, callback) => {
      if (value === this.loginForm.newPassword) {
        callback()
      } else {
        callback(new Error('两次密码输入的不一致,请重新填写'))
      }
    }
    return {
      loginForm: {
        id: '',
        oldPassword: '',
        newPassword: '',
        surepassword: ''

      },
      loginRules: {
        password: [
          { required: true, trigger: 'blur', validator: validatePassword },
          { min: 8, max: 20, message: '密码由8-20个大小写字母、数字、特殊字符组成' },
          { pattern: /^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9])).{8,20}$/, message: '密码由8-20个大小写字母、数字、特殊字符组成' }
        ],
        newpassword: [
          { required: true, trigger: 'blur', validator: validatePassword },
          { min: 8, max: 20, message: '密码由8-20个大小写字母、数字、特殊字符组成' },
          { pattern: /^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9])).{8,20}$/, message: '密码由8-20个大小写字母、数字、特殊字符组成' }
        ],
        surepassword: [
          { required: true, trigger: 'blur', validator: validateNewPassword }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    // $route: {
    //   handler: function(route) {
    //     this.redirect = route.query && route.query.redirect
    //   },
    //   immediate: true
    // }
  },
  mounted() {
  },
  methods: {
    sureLogin() {
      this.loginForm.id = Number(this.$route.query.id)
      this.$refs.loginForm.validate(async(valid) => {
        if (valid) {
          this.loading = true
          const res = await updatePassword(this.loginForm)
          if (res.code === 200) {
            Message({
              message: '密码修改成功!',
              type: 'success',
              showClose: true,
              duration: 2 * 1000
            })
            this.$router.push('/login')
            this.loading = false
          } else {
            this.loading = false
          }
        } else {
          this.loading = false
          return false
        }
      })
    },
    backLogin() {
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */

$bg:#2d3a4b;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 0 104px 0;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    // border: 1px solid rgba(255, 255, 255, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    // border-radius: 5px;
    color: #D3D3D3;
    border-bottom: 1px solid rgba(221,221,221, 0.1);
  }
  // .el-checkbox__inner{
  //   width:12px;
  //   height:12px;
  //   border-radius:2px;
  //   border:1px solid rgba(19,156,217,1);
  //   background-color: transparent;
  // }
}
</style>

<style lang="scss" scoped>
$bg:#072041;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background: url('../../assets/images/login-bg.png') no-repeat;
  background-size:100% 100%;
  -moz-background-size:100% 100%;
  // display: flex;
  .el-checkbox{
    margin-left: 16px;
    color: #15A7E5;
    margin-bottom: 10px;
  }

  .login-form {
    // position: relative;
    width: 362px;
    max-width: 100%;
    padding: 35px 35px 0 35px;
    // margin: 0 auto;
    // margin-right: 277px;
    background: url('../../assets/images/login-input.png') no-repeat;
    background-size:100% 100%;
   -moz-background-size:100% 100%;
    overflow: hidden;
     margin: 0 auto;
    margin-top: 88px;
    // margin-right: 20%;
    .el-button--primary{
      // background-color: #337DFF;
      // border-color: #337DFF;
      background:#F7B500;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    // flex: 1;
    .title {
          height: 36px;
          font-size: 29px;
          color: #fff;
          line-height: 41px;
          margin: 41px 0 41px 60px;
    }
  }
  .content{
    display: flex;
    .left{
      flex: 1;
      margin: 70px 0 0 32px;
      img{
         width: 710px;
      }
    }
    .right{
      flex: 1;
      .title-img{
        width: 292px;
        margin: 0 auto;
        padding: 0px 0 20px 0;
      }
      .captcha_code{
        float: right;
        position: absolute;
        margin: -42px 0px 0px 208px;
      }
    }

  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
